package com.shuo.dsa.leetcode;

import com.shuo.dsa.model.TreeNode;

import java.util.ArrayList;
import java.util.Stack;

/**
 * 先序遍历:
 *      思路: 直接背
 */
public class BinTreePreOrder {
    public ArrayList<Integer> preorderTraversal(TreeNode root) {
        if (root == null) return new ArrayList<>();
        ArrayList<Integer> ret = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()) {
            TreeNode node =  stack.pop();
            ret.add(node.value);
            if (node.right != null) stack.push(node.right);
            if (node.left != null) stack.push(node.left);
        }
        return ret;
    }
}
